package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.RorderDao;
import dao.ViewDao;
import dao.VrouteDao;
import model.Rorder;
import model.View;
import model.Vroute;
import util.CharSet;

/**
 * Servlet implementation class VrouteServlet
 */
@WebServlet("/VrouteServlet")
public class VrouteServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public VrouteServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		// response.getWriter().append("Served at: ").append(request.getContextPath());
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String handle = request.getParameter("handle");
		if (handle != null && handle.equals("delVroute")) {
			String vname = CharSet.getNewString(request.getParameter("vname"));
			String rname = CharSet.getNewString(request.getParameter("rname"));
			Vroute vroute;
			try {
				vroute = VrouteDao.newVroute(rname, vname);
				if (VrouteDao.getVrouteNoCacheForBool(vroute)) {
					VrouteDao.delVroute(vroute);
					String flag = "1";
					request.setAttribute("flag", flag);
					request.getRequestDispatcher("/admin/AdminRouteList.jsp").forward(request, response);
				} else {
					String flag = "0";
					request.setAttribute("flag", flag);
					request.getRequestDispatcher("/admin/AdminRouteList.jsp").forward(request, response);
				}
			} catch (Exception e) {
				String flag = "0";
				request.setAttribute("flag", flag);
				request.getRequestDispatcher("/admin/AdminRouteList.jsp").forward(request, response);
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (handle != null && handle.equals("addVroute")) {
			String vname = CharSet.getNewString(request.getParameter("vname"));
			String rname = CharSet.getNewString(request.getParameter("rname"));
			Vroute vroute;
			try {
				//给对象赋值
				vroute = VrouteDao.newVroute(rname, vname);
				if (!VrouteDao.getVrouteNoCacheForBool(vroute)) {
					VrouteDao.addVroute(vroute);
					if (VrouteDao.getVrouteNoCacheForBool(vroute)) {
						String flag = "1";
						request.setAttribute("flag", flag);
						request.getRequestDispatcher("/admin/AdminAddVroutList.jsp").forward(request, response);
					} else {
						String flag = "0";
						request.setAttribute("flag", flag);
						request.getRequestDispatcher("/admin/AdminAddVroutList.jsp").forward(request, response);
					}

				} else {
					String flag = "0";
					request.setAttribute("flag", flag);
					request.getRequestDispatcher("/admin/AdminAddVroutList.jsp").forward(request, response);
				}
			} catch (Exception e) {
				String flag = "0";
				request.setAttribute("flag", flag);
				request.getRequestDispatcher("/admin/AdminAddVroutList.jsp").forward(request, response);
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
